//
//  CZBaseViewController.swift
//  crkWeibo
//
//  Created by 陈荣科 on 15/12/16.
//  Copyright © 2015年 crk. All rights reserved.
//

import UIKit

class CZBaseViewController: UITableViewController,CZVistorViewDelegate {

    var login = CZUserAccount.userLogin
    
    override func loadView() {
        login ? super.loadView() : showView()
    }
    
    
    func showView() {
        
        view = visitorView
        
        
        //添加导航button的按钮
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "注册", style: UIBarButtonItemStyle.Plain, target: self, action: "visitorViewRegisterClick")
        
        
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "登录", style: UIBarButtonItemStyle.Plain, target: self, action: "visitorViewLoginClick")
        
        //设置代理
        //当对象没有实现协议,会报错
        //实现协议后还会报错，需要实现协议里面的方法
        visitorView.delegate = self
        
        //判断是哪个访客试图的控制器
        if self is CZHomeViewController {
        
            //首页动画旋转
            visitorView.startRotationAnimation()
            
            //程序退出后台和进入前台调用着通知
            //通知
            NSNotificationCenter.defaultCenter().addObserver(self, selector: "didEnterBackground", name: UIApplicationDidEnterBackgroundNotification, object: nil)
            
            NSNotificationCenter.defaultCenter().addObserver(self, selector: "didBecomeActive", name: UIApplicationDidBecomeActiveNotification, object: nil)
            
            
        }else if self is CZMessageViewController {
            
            visitorView.setupVisitorInfo("visitordiscover_image_message", message: "登录后，别人评论你的微博，发给你的消息，都会在这里收到通知")
            
        }else if self is CZDiscoveryViewController {
        
            visitorView.setupVisitorInfo("visitordiscover_image_message", message: "登录后，最新、最热微博尽在掌握，不再会与实事潮流擦肩而过")
        }else if self is CZProfileViewController {
        
            visitorView.setupVisitorInfo("visitordiscover_image_profile", message:  "登录后，你的微博、相册、个人资料会显示在这里，展示给别人")
        
        }
        
    }

    func visitorViewRegisterClick() {
        print(__FUNCTION__)
    }
    
    func visitorViewLoginClick() {
        
        //创建登录界面
        let oauthVC = CZOauthViewController()
        
        //弹出登录界面，包装导航控制器
        
        presentViewController(UINavigationController(rootViewController: oauthVC), animated: true, completion: nil)
    }

    
    //停止动画
    func didEnterBackground() {
     
        visitorView.pauseAnimation()
    }
    
    //继续动画
    func didBecomeActive () {
    
        visitorView.resumeAnimation()
    
    }
    
  //懒加载
    
    lazy var visitorView: CZVistorView = CZVistorView()
    
}
